<?php
namespace wap\controllers;

use home\models\SigninForm;
use home\models\SignupForm;
use home\models\Users;
use libs\Utils;
use Yii;
use yii\web\Response;

/**
 * Class UserController
 *
 * @package home\controllers
 */
class UserController extends BaseController
{

    public function actionSignin()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;

        $model = new SigninForm();
        $model->load(Yii::$app->request->post(), '');

        if($user = $model->signin()){
            if(is_object($user)){
                Yii::$app->user->login($user);

                return [
                    'error' => 0,
                    'msg' => '登录成功！',
                ];
            }
        }

        return [
            'error' => 1,
            'msg' => print_r($user, true),
        ];
    }

    public function actionSignup()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;

        $model = new SignupForm();
        $model->load(Yii::$app->request->post(), '');

        if($user = $model->signup()){
            Yii::$app->user->login($user);

            return [
                'error' => 0,
                'msg' => '注册成功！',
            ];
        }

        $errors = $model->getFirstErrors();

        return [
            'error' => 1,
            'msg' => join(",", $errors),
        ];
    }

    public function actionReset()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;

        $model = new ResetForm();
        $model->load(Yii::$app->request->post(), '');

        if($model->reset()){
            return [
                'message' => '密码重置成功！',
            ];
        }else{
            throw new BadRequestHttpException(\libs\Utils::paserErrors($model->getFirstErrors()));
        }
    }

    public function actionVcode()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;

        $mobile = Yii::$app->request->post('mobile');
        $event = Yii::$app->request->post('event');

        $last_send = Yii::$app->session->get('last_send', 0);

        if(time() - $last_send < 60){
            return [
                'error' => 1,
                'msg' => '发送太频繁！',
            ];
        }

        $template = 'SMS_149325072';

        if($event === 'signup'){
            $template = 'SMS_149325069';
        }else if($event === 'reset'){
            $template = 'SMS_149325068';
        }else if($event === 'signin'){
            $template = 'SMS_149325071';
        }

        if(!Utils::checkMobile($mobile)){
            return [
                'error' => 1,
                'msg' => '手机号码格式不正确。',
            ];
        }

        if($event === 'signup' && Users::find()->where(['mobile' => $mobile])->exists()){
            return [
                'error' => 1,
                'msg' => '手机号码已注册。',
            ];
        }

        if($event === 'reset' && !Users::find()->where(['mobile' => $mobile])->exists()){
            return [
                'error' => 1,
                'msg' => '手机号码不存在。',
            ];
        }

        $vcode = rand(100000, 999999);
        try{
            $result = Yii::$app->sms->sendSms($mobile, $template, ['code' => $vcode]);
        }catch(\Exception $e){
            return [
                'error' => 1,
                'msg' => '发送失败！',
            ];
        }

        if($result->Code != 'OK'){
            return [
                'error' => 1,
                'msg' => $result->Message,
            ];
        }

        Yii::$app->cache->set('api_vcode_' . $mobile, $vcode, 1800);
        Yii::$app->session->set('last_send', time());

        return [
            'error' => 0,
            'vcode' => $vcode
        ];
    }
}
